package com.molinari.jdbcandroid.wrap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import com.molinari.jdbcandroid.interpreter.InterpretatoreSqlString;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;


public class StatementAndroid implements Statement {

	private SQLiteStatement statementAndroid;
	private ConnectionAndroid cn;
	private ResultSetAndroid rs;

	StatementAndroid() {

	}

	StatementAndroid(final ConnectionAndroid cn) {
		this.cn = cn;
	}

	@Override
	public void addBatch(final String sql) throws SQLException {
		throw new ExceptionDB(null, null);

	}

	@Override
	public void cancel() throws SQLException {
		System.out.println("Funzione non implementata");
	}

	private void closeResultSet() throws SQLException {
		if (rs != null) {
			rs.close();
			rs = null;
		}
	}

	@Override
	public void clearBatch() throws SQLException {
		System.out.println("Funzione non implementata");

	}

	@Override
	public void clearWarnings() throws SQLException {
		System.out.println("Funzione non implementata");

	}

	@Override
	public void close() throws SQLException {
		closeResultSet();
		statementAndroid.close();

	}

	@Override
	public boolean execute(final String sql) throws SQLException {
		closeResultSet();
		final String operazione = InterpretatoreSqlString.getOperazioneDaSqlString(sql);
		if (!operazione.equalsIgnoreCase("SELECT")) {
			cn.getDb().execSQL(sql);
			return true;
		} else {
			final Cursor cursore = cn.getDb().rawQuery(sql, null);
			rs = new ResultSetAndroid(cursore);
			return true;
		}
	}

	@Override
	public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public boolean execute(final String sql, final String[] columnNames) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public int[] executeBatch() throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public ResultSet executeQuery(final String sql) throws SQLException {
		closeResultSet();
		final Cursor cursore = cn.getDb().rawQuery(sql, null);
		rs = new ResultSetAndroid(cursore);
		return rs;
	}

	public static String[] trovaOggettoFromStringSQL(final String SQL, String nome, final String partenza, final String fine, String splitter) {
		final String sql = SQL.toLowerCase();
		if (nome != null && splitter != null) {
			nome = nome.toLowerCase();
			splitter = splitter.toLowerCase();
			if (sql.contains(nome)) {
				final String[] sqlSplittato = sql.split(nome);
				String per = sqlSplittato[1];
				per = per.substring(1);
				int indice = per.indexOf(" ");
				indice = indice == -1 ? per.indexOf(splitter) : indice;
				indice = indice == -1 ? per.length() : indice;
				return new String[] { per.substring(0, indice) };
			}
		} else if (partenza != null && fine != null && splitter != null) {

		}
		return null;
	}

	@Override
	public int executeUpdate(final String sql) throws SQLException {
		closeResultSet();
		cn.getDb().execSQL(sql);
		return 1;
	}

	public static String getOperazioneDaSqlString(String sql) {
		sql = sql.toLowerCase();
		final String[] split = sql.split(" ");
		return split[0];
	}

	@Override
	public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public Connection getConnection() throws SQLException {
		return cn;
	}

	@Override
	public int getFetchDirection() throws SQLException {
		return ResultSet.FETCH_FORWARD;

	}

	@Override
	public int getFetchSize() throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public ResultSet getGeneratedKeys() throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public int getMaxFieldSize() throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public int getMaxRows() throws SQLException {
		throw new ExceptionDB(null, null);
	}

	@Override
	public boolean getMoreResults() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean getMoreResults(final int current) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getQueryTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ResultSet getResultSet() throws SQLException {
		return rs;
	}

	@Override
	public int getResultSetConcurrency() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getResultSetHoldability() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getResultSetType() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getUpdateCount() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setCursorName(final String name) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEscapeProcessing(final boolean enable) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setFetchDirection(final int direction) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setFetchSize(final int rows) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setMaxFieldSize(final int max) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setMaxRows(final int max) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setQueryTimeout(final int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isWrapperFor(Class<?> arg0) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> arg0) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isClosed() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isPoolable() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setPoolable(boolean poolable) throws SQLException {
		// TODO Auto-generated method stub
		
	}

}
